#include "pthreadpool.h"
#include "function_s.h"
#include <func.h>

void pthreadpool_creat(pthreadpool_t* q)
{
    q->index=(pthread_t*)calloc(PTHREAD_NUM,sizeof(pthread_t));
    q->pthreadnum = PTHREAD_NUM;
    queue_init(&q->queue);
}

void pthreadpool_destroy(pthreadpool_t* p)
{ 
    queue_destroy(&p->queue);
    free(p->index);
}

void pthreadpool_start(pthreadpool_t *p)
{
    for(int i=0;i<p->pthreadnum;i++){
        pthread_create(&p->index[i],NULL,pthreadpool_handle,&p->queue);
    }
}

void* pthreadpool_handle(void* arg)
{
    taskqueue_t* t=(taskqueue_t*)arg;
    while(1){
        int fd=dequeue(t);
        printf("%d client has been reserve!\n",fd);
        char buf[1024];
        char buf1[1024];
        char buf2[1024];
        int ch;
        if(fd!=-1){
            while(1){
                int ret=1;
                memset(buf,0,1024);
                memset(buf1,0,1024);
                memset(buf2,0,1024);
                int length;
                ret=recv(fd,&length,4,0);
                recv(fd,buf,length,MSG_WAITALL);
                if(ret==0){ 
                    close(fd);
                    break;
                }
                else if(ret==-1&&errno==EINTR) continue;
                else if(ret==-1){ 
                    perror("recv");
                    close(fd);
                    continue;
                } 
                sscanf(buf,"%d %s %s",&ch,buf1,buf2);
                puts(buf1);
                puts(buf2);
                switch(ch){
                case CMD_TYPE_EXIT:
                    printf("client has exit!\n");
                    ret=0;
                    break;
                case CMD_TYPE_CD:
                    printf("client has cd!\n");
                    ret=cd_s(fd,buf1);
                    break;
                case CMD_TYPE_LS:
                    printf("client has ls!\n");
                    ret=ls_s(fd,buf1);
                    break;
                case CMD_TYPE_LL:
                    printf("client has ll!\n");
                    ret=ll_s(fd,buf1);
                    break;
                case CMD_TYPE_PWD:
                    printf("client has pwd!\n");
                    ret=pwd_s(fd);
                    break;
                case CMD_TYPE_GETSFILE:
                    printf("client has getsfile!\n");
                    ret=getsfile_s(fd,buf1);
                    break;
                case CMD_TYPE_PUTSFILE:
                    printf("client has putsfile!\n");
                    ret=putsfile_s(fd);
                    break;
                case CMD_TYPE_MV:
                    printf("client has mv!\n");
                    ret=mv_s(buf1,buf2);
                    break;
                case CMD_TYPE_CP:
                    printf("client has cp!\n");
                    ret=cp_s(buf1,buf2);
                    break;
                case CMD_TYPE_RM:
                    printf("client has rm!\n");
                    ret=rm_s(buf1);
                    break;
                case CMD_TYPE_RMDIR:
                    printf("client has rmdir!\n");
                    ret=rmdir_s(buf1);
                    break;
                case CMD_TYPE_MKDIR:
                    printf("client has mkdir!\n");
                    ret=mkdir_s(buf1);
                    break;
                default:
                    break; 
                }
                if(ret==-1) send(fd,"ERROR ORDER!",13,MSG_WAITALL);
                else if(ret==0){
                    close(fd);
                    break;
                }
            }
        }else break;
    }
    printf("pthread %ld is exiting!\n",pthread_self());
    pthread_exit(0);
}

